package com.jiyun.springboot_user.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import sys.dao.TbUserDao;
import sys.entity.PageData;
import sys.entity.TbUserEntity;
import sys.service.TbUserService;
import sys.utils.ExcelUtil;
import sys.utils.PageUtil;

import javax.servlet.http.HttpServletResponse;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

@Service
public class TbUserServiceImpl implements TbUserService {


	private final TbUserDao dao;

	@Autowired
	public TbUserServiceImpl(TbUserDao dao) {
		this.dao = dao;
	}

	@Override
	public void add(TbUserEntity entity) {
		dao.add(entity);
	}

	@Override
	public void delete(TbUserEntity entity) {
		dao.delete(entity);
	}

	@Override
	public void update(TbUserEntity entity) {
		dao.update(entity);
	}

	@Override
	public List<TbUserEntity> select(TbUserEntity entity) {
		return dao.select(entity);
	}

	@Override
	public PageData<TbUserEntity> likeSelect(TbUserEntity entity) {
		return PageUtil.getPageData(entity, dao);
	}

	@Override
	public void exportExcel(TbUserEntity entity, HttpServletResponse response) {

		// 获取头部信息（可以设置为动态）
		String[] headList = new String[] { "主键", "用户名", "密码", "创建时间", "更新时间", "1.青铜2.白银3.黄金", "1男0女", "角色id", "email", "phone", "hobby"};

		String[] headEngList = new String[]{ "id", "username", "password", "createTime", "updateTime", "userLevel", "sex", "roleId", "email", "phone", "hobby"};

		String[] describeList = new String[] { "", "", "", "", "", "", "", "", "", "", ""};

		//设置头部以及描述信息
        Map<String, String> headAndDescribeMap = new LinkedHashMap<>();
        for (int i = 0; i < headEngList.length; i++) {
            headAndDescribeMap.put(headEngList[i], describeList[i]);
        }

		ExcelUtil.exportExcel(entity, response, dao, headList, headAndDescribeMap);
	}
}
